package com.sailfish.springbootdemo.service.db1;

import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.configs.UserHeaderHolder;
import com.sailfish.springbootdemo.dao.db1.BaseCapacityRuleDao;
import com.sailfish.springbootdemo.dao.db1.LineBaseBigTypeDao;
import com.sailfish.springbootdemo.dao.db1.LineBaseParamDao;
import com.sailfish.springbootdemo.dao.db1.LineBaseSmallTypeDao;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.LineBaseBigType;
import com.sailfish.springbootdemo.pojo.db1.LineBaseSmallType;
import com.sailfish.springbootdemo.pojo.db4.Operate;
import com.sailfish.springbootdemo.service.db4.OperateService;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class LineBaseBigTypeServiceImpl implements LineBaseBigTypeService {

    @Autowired
    private LineBaseBigTypeDao lineBaseBigTypeDao;

    @Autowired
    private LineBaseSmallTypeDao lineBaseSmallTypeDao;

    @Autowired
    private LineBaseParamDao lineBaseParamDao;

    @Autowired
    private BaseCapacityRuleDao baseCapacityRuleDao;

    @Autowired
    private CommonJudgeCanDeleteService commonService;

    @Autowired
    private OperateService operateService;

    private String operateSmallType = "基线整线项目大类";

    @Override
    public List<LineBaseBigType> getAllLineBaseBigType(String searchValue) {
        List<LineBaseBigType> list = lineBaseBigTypeDao.getAllLineBaseBigType(searchValue);
        for (LineBaseBigType lineBaseBigType : list) {
            List<LineBaseSmallType> allLineBaseSmallTypeByBigType = lineBaseSmallTypeDao.getAllLineBaseSmallTypeByBigType("", lineBaseBigType.getLineBaseBigTypeId());
            lineBaseBigType.setLineBaseSmallTypeList(allLineBaseSmallTypeByBigType);
        }
        return list;
    }

    public List<LineBaseBigType> getAllLineBaseBigTypeAndSmallAndRule() {
        List<LineBaseBigType> list = lineBaseBigTypeDao.getAllLineBaseBigType("");
        for (LineBaseBigType lineBaseBigType : list) {
            List<LineBaseSmallType> allLineBaseSmallTypeByBigType = lineBaseSmallTypeDao.getAllLineBaseSmallTypeByBigType("", lineBaseBigType.getLineBaseBigTypeId());
            for (LineBaseSmallType lineBaseSmallType : allLineBaseSmallTypeByBigType) {
                lineBaseSmallType.setBaseCapacityRuleList(baseCapacityRuleDao.getAllBaseCapacityRule(null, lineBaseBigType.getLineBaseBigTypeId(), lineBaseSmallType.getLineBaseSmallTypeId()));
                //lineBaseSmallType.setLineBaseParamList(lineBaseParamDao.getAllLineBaseParamByType("", lineBaseBigType.getLineBaseBigTypeId(), lineBaseSmallType.getLineBaseSmallTypeId()));
            }
            lineBaseBigType.setLineBaseSmallTypeList(allLineBaseSmallTypeByBigType);
        }
        return list;
    }

    @Override
    public Result addOrUpdateLineBaseBigType(LineBaseBigType lineBaseBigType, String mode) {
        try {
            if (lineBaseBigType == null) {
                return ResultUtil.error(500, "缺少参数lineBaseBigType", "Miss Param lineBaseBigType", null);
            }
            if (mode == null || "".equals(mode)) {
                return ResultUtil.error(500, "缺少参数mode", "Miss Param mode", null);
            }
            if (!("edit".equals(mode) || "add".equals(mode))) {
                return ResultUtil.error(500, "mode 参数错误", "mode Param Error", null);
            }

            String userId = UserHeaderHolder.getUserId();

            if ("edit".equals(mode)) {
                if (lineBaseBigType.getLineBaseBigTypeId() == null || "".equals(lineBaseBigType.getLineBaseBigTypeId().toString())) {
                    return ResultUtil.error(500, "缺少参数lineBaseBigTypeId", "Miss Param lineBaseBigTypeId", null);
                }

                List<LineBaseBigType> lineBaseBigTypeList = lineBaseBigTypeDao.judgeUniqueLineBaseBigType(lineBaseBigType.getLineBaseBigTypeId(), lineBaseBigType.getLineBaseBigTypeName());
                if (lineBaseBigTypeList.size() > 0) {
                    return ResultUtil.error(500, "修改失败，存在相同名字", "Operate Fail", null);
                }

                LineBaseBigType oldLineBaseBigType = lineBaseBigTypeDao.getLineBaseBigTypeById(lineBaseBigType.getLineBaseBigTypeId());

                lineBaseBigType.setUpdateTime(new Date());
                Integer integer = lineBaseBigTypeDao.updateLineBaseBigType(lineBaseBigType);
                if (integer > 0) {
                    // 操作记录
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 3, 1, new Date(), JSONObject.toJSONString(oldLineBaseBigType), JSONObject.toJSONString(lineBaseBigType)));
                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            } else {

                List<LineBaseBigType> lineBaseBigTypeByName = lineBaseBigTypeDao.getLineBaseBigTypeByName(lineBaseBigType.getLineBaseBigTypeName());
                if (lineBaseBigTypeByName.size() > 0) {
                    return ResultUtil.error(500, "该类型已存在", "Already Exist", null);
                }

                lineBaseBigType.setCreateTime(new Date());
                Integer integer = lineBaseBigTypeDao.addLineBaseBigType(lineBaseBigType);
                if (integer > 0) {
                    // 操作记录
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 1, 1, new Date(), null, JSONObject.toJSONString(lineBaseBigType)));
                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result deleteLineBaseBigType(Integer lineBaseBigTypeId) {
        try {
            if (lineBaseBigTypeId == null || "".equals(lineBaseBigTypeId.toString())) {
                return ResultUtil.error(500, "缺少参数lineBaseBigTypeId", "Miss Param lineBaseBigTypeId", null);
            }

            String userId = UserHeaderHolder.getUserId();
            LineBaseBigType oldLineBaseBigType = lineBaseBigTypeDao.getLineBaseBigTypeById(lineBaseBigTypeId);

            Map<String, Integer> stringIntegerMap = commonService.judgeCanDeleteLineBaseBigType(lineBaseBigTypeId);
            if (stringIntegerMap.get("lineBaseSmallTypeNum") > 0 || stringIntegerMap.get("baseCapacityRuleNum") > 0) {
                return ResultUtil.error(500,
                        "禁止删除，当前项" + stringIntegerMap.get("lineBaseSmallTypeNum") + "处关联项目小类，" + stringIntegerMap.get("baseCapacityRuleNum") + "处关联基线整线能力规则",
                        "Forbidden Delete", null);
            }
            Integer integer = lineBaseBigTypeDao.deleteLineBaseBigType(lineBaseBigTypeId);
            if (integer > 0) {
                // 操作记录
                operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 2, 1, new Date(), JSONObject.toJSONString(oldLineBaseBigType), null));
                return ResultUtil.success(null);
            }
            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }
}
